#include <iostream>
#include <thread>
#include <mutex>

class SafeCounter {
public: 
    SafeCounter(): _value(0) {}

    void increment() {
        std::lock_guard<std::mutex> lock(_mutex);
        _value++;
    }

    int getvalue() {
        std::lock_guard<std::mutex> lock(_mutex);
        return _value;
    }

private:
    int _value;
    std::mutex _mutex;
};

int main() {
    SafeCounter counter;

    auto increcount = [&counter]() {
        for (int i = 0; i < 100; ++i) {
            counter.increment();
        }
    };

    std::thread t1(increcount);
    std::thread t2(increcount);

    t1.join();
    t2.join();

    std::cout << "Final value is: " << counter.getvalue() << std::endl;

    return 0;
}